{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:27.308719Z",
     "iopub.status.busy": "2023-11-13T13:30:27.308531Z",
     "iopub.status.idle": "2023-11-13T13:30:28.630088Z",
     "shell.execute_reply": "2023-11-13T13:30:28.629433Z",
     "shell.execute_reply.started": "2023-11-13T13:30:27.308695Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import lightgbm as lgb\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from sklearn.metrics import f1_score\n",
    "from sklearn.model_selection import StratifiedKFold, cross_val_score\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "def local_f1(y_pred, y_true): \n",
    "    list_pred = y_pred['TD_IND'].to_list()\n",
    "    list_pred.extend(y_pred['FNCG_IND'].to_list())\n",
    "    list_pred.extend(y_pred['FUND_IND'].to_list())\n",
    "    list_pred.extend(y_pred['INSUR_IND'].to_list())\n",
    "    list_pred.extend(y_pred['IL_IND'].to_list())\n",
    "    \n",
    "    list_true = y_true['TD_IND'].to_list() \n",
    "    list_true.extend(y_true['FNCG_IND'].to_list()) \n",
    "    list_true.extend(y_true['FUND_IND'].to_list())\n",
    "    list_true.extend(y_true['INSUR_IND'].to_list())\n",
    "    list_true.extend(y_true['IL_IND'].to_list())\n",
    "    return f1_score(list_pred ,list_true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:28.631426Z",
     "iopub.status.busy": "2023-11-13T13:30:28.631231Z",
     "iopub.status.idle": "2023-11-13T13:30:28.796481Z",
     "shell.execute_reply": "2023-11-13T13:30:28.795898Z",
     "shell.execute_reply.started": "2023-11-13T13:30:28.631401Z"
    }
   },
   "outputs": [],
   "source": [
    "run ./functionb.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:28.797971Z",
     "iopub.status.busy": "2023-11-13T13:30:28.797768Z",
     "iopub.status.idle": "2023-11-13T13:30:28.801267Z",
     "shell.execute_reply": "2023-11-13T13:30:28.800673Z",
     "shell.execute_reply.started": "2023-11-13T13:30:28.797947Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "stage = 'B'\n",
    "train_path = '../../contest/train'\n",
    "stage_path = '../../contest/{}'.format(stage)\n",
    "data_path = './data'\n",
    "\n",
    "labels = ['TD_IND', 'FNCG_IND', 'FUND_IND', 'INSUR_IND', 'IL_IND']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:28.802402Z",
     "iopub.status.busy": "2023-11-13T13:30:28.802230Z",
     "iopub.status.idle": "2023-11-13T13:30:28.899268Z",
     "shell.execute_reply": "2023-11-13T13:30:28.898727Z",
     "shell.execute_reply.started": "2023-11-13T13:30:28.802380Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "df_train = pd.read_csv(os.path.join(train_path,'GSLD_TARGET_TRAIN.csv'))\n",
    "df_test = pd.read_csv(os.path.join(stage_path,'GSLD_TARGET_{}.csv'.format(stage)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:28.900255Z",
     "iopub.status.busy": "2023-11-13T13:30:28.900080Z",
     "iopub.status.idle": "2023-11-13T13:30:31.389744Z",
     "shell.execute_reply": "2023-11-13T13:30:31.389109Z",
     "shell.execute_reply.started": "2023-11-13T13:30:28.900234Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GSLD_NATURE_CUST_B.csv (5000, 4)\n",
      "GSLD_MB_BASICS_B.csv (5000, 7)\n",
      "GSLD_MB_TRNFLW_B.csv (5000, 15)\n",
      "GSLD_MB_QRYTRNFLW_B.csv (5000, 20)\n",
      "GSLD_AGET_PAY_B.csv (5000, 26)\n",
      "GSLD_ASSET_DEBT_B.csv (5000, 86)\n",
      "add_aget_diff_B (5000, 88)\n",
      "GSLD_TR_APS_B.csv (5000, 144)\n"
     ]
    }
   ],
   "source": [
    "del df_test['DATA_DAT'], df_train['DATA_DAT']\n",
    "\n",
    "for file in ['GSLD_NATURE_CUST.csv', 'GSLD_MB_BASICS.csv'\n",
    "             , 'GSLD_MB_TRNFLW.csv', 'GSLD_MB_QRYTRNFLW.csv'\n",
    "             , 'GSLD_AGET_PAY.csv' \n",
    "             , 'GSLD_ASSET_DEBT.csv'\n",
    "#              , 'wtfxb_bb_derivative'\n",
    "             , 'add_aget_diff'\n",
    "#              , 'add_aps_aget'\n",
    "             , 'GSLD_TR_APS.csv']:\n",
    "#             , 'tmpfxb_bb_aget_interval']:\n",
    "    df_tmp = pd.read_csv(os.path.join(data_path, file))\n",
    "    df_train = df_train.merge(df_tmp,on='CUST_NO',how='left')\n",
    "    \n",
    "for file in ['GSLD_NATURE_CUST_B.csv', 'GSLD_MB_BASICS_B.csv'\n",
    "             , 'GSLD_MB_TRNFLW_B.csv', 'GSLD_MB_QRYTRNFLW_B.csv'\n",
    "             , 'GSLD_AGET_PAY_B.csv'\n",
    "            , 'GSLD_ASSET_DEBT_B.csv'\n",
    "#              , 'wtfxb_bb_derivative'\n",
    "             , 'add_aget_diff_B'\n",
    "#              , 'add_aps_aget_B'\n",
    "            , 'GSLD_TR_APS_B.csv']:\n",
    "#             , 'tmpfxb_bb_aget_interval']:\n",
    "    df_tmp = pd.read_csv(os.path.join(data_path, file))\n",
    "    df_test = df_test.merge(df_tmp,on='CUST_NO',how='left')\n",
    "    print(file, df_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:31.390862Z",
     "iopub.status.busy": "2023-11-13T13:30:31.390670Z",
     "iopub.status.idle": "2023-11-13T13:30:31.393365Z",
     "shell.execute_reply": "2023-11-13T13:30:31.392771Z",
     "shell.execute_reply.started": "2023-11-13T13:30:31.390838Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# def show_nullrow_count(df_train):\n",
    "#     for col in df_train.columns:\n",
    "#         print(col, df_train[col].isnull().any(), df_train[df_train[col].isnull()].shape[0])\n",
    "\n",
    "# show_nullrow_count(df_train)\n",
    "# show_nullrow_count(df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:31.394213Z",
     "iopub.status.busy": "2023-11-13T13:30:31.394052Z",
     "iopub.status.idle": "2023-11-13T13:30:31.398979Z",
     "shell.execute_reply": "2023-11-13T13:30:31.398306Z",
     "shell.execute_reply.started": "2023-11-13T13:30:31.394192Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "labels = ['TD_IND','FNCG_IND','FUND_IND','INSUR_IND','IL_IND']\n",
    "features = [x for x in df_train.columns if x not in labels+['label', 'DATA_DAT', 'CUST_NO', 'AGET_5DAYLS_RATIO']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:31.399770Z",
     "iopub.status.busy": "2023-11-13T13:30:31.399606Z",
     "iopub.status.idle": "2023-11-13T13:30:31.404554Z",
     "shell.execute_reply": "2023-11-13T13:30:31.404061Z",
     "shell.execute_reply.started": "2023-11-13T13:30:31.399747Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5000, 144)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:31.405395Z",
     "iopub.status.busy": "2023-11-13T13:30:31.405230Z",
     "iopub.status.idle": "2023-11-13T13:30:31.408757Z",
     "shell.execute_reply": "2023-11-13T13:30:31.408289Z",
     "shell.execute_reply.started": "2023-11-13T13:30:31.405373Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(60000, 149)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:31.409578Z",
     "iopub.status.busy": "2023-11-13T13:30:31.409416Z",
     "iopub.status.idle": "2023-11-13T13:30:35.805637Z",
     "shell.execute_reply": "2023-11-13T13:30:35.804975Z",
     "shell.execute_reply.started": "2023-11-13T13:30:31.409556Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用最大最小值标准化连续特征 NTRL_CUST_SEX_CD\n",
      "使用最大最小值标准化连续特征 NTRL_CUST_AGE\n",
      "使用最大最小值标准化连续特征 NTRL_RANK_CD\n",
      "使用最大最小值标准化连续特征 MB_REG_PROV\n",
      "使用最大最小值标准化连续特征 MB_REG_TIME_LAPSE\n",
      "使用最大最小值标准化连续特征 MS_IND\n",
      "使用最大最小值标准化连续特征 trnflw_trn_cd_nunique\n",
      "使用最大最小值标准化连续特征 trnflw_last_date\n",
      "使用最大最小值标准化连续特征 trnflw_mean_amount\n",
      "使用最大最小值标准化连续特征 trnflw_sum_amount\n",
      "使用最大最小值标准化连续特征 trnflw_exist_weekend\n",
      "使用最大最小值标准化连续特征 trnflw_cust_cnt\n",
      "使用最大最小值标准化连续特征 trnflw_weekend_cnt\n",
      "使用最大最小值标准化连续特征 trnflw_weekend_ratio\n",
      "使用最大最小值标准化连续特征 qrytrnflw_last_date\n",
      "使用最大最小值标准化连续特征 qry_trn_cd_nunique\n",
      "使用最大最小值标准化连续特征 qrytrnflw_cust_cnt\n",
      "使用最大最小值标准化连续特征 qrytrnflw_weekend_cnt\n",
      "使用最大最小值标准化连续特征 qrytrnflw_weekend_ratio\n",
      "使用最大最小值标准化连续特征 cust_cnt\n",
      "使用最大最小值标准化连续特征 tr_amt_sum\n",
      "使用最大最小值标准化连续特征 tr_amt_max\n",
      "使用最大最小值标准化连续特征 prov_cd\n",
      "使用最大最小值标准化连续特征 unit_typ_cd\n",
      "使用最大最小值标准化连续特征 tr_amt_avg\n",
      "使用最大最小值标准化连续特征 AST_DAY_FA_BAL\n",
      "使用最大最小值标准化连续特征 AST_MAVER_FA_BAL\n",
      "使用最大最小值标准化连续特征 AST_SAVER_FA_BAL\n",
      "使用最大最小值标准化连续特征 AST_YAVER_FA_BAL\n",
      "使用最大最小值标准化连续特征 AST_FA_BAL_MAX\n",
      "使用最大最小值标准化连续特征 AST_DAY_DP_BAL\n",
      "使用最大最小值标准化连续特征 AST_MAVER_DP_BAL\n",
      "使用最大最小值标准化连续特征 AST_SAVER_DP_BAL\n",
      "使用最大最小值标准化连续特征 AST_YAVER_DP_BAL\n",
      "使用最大最小值标准化连续特征 AST_DP_BAL_MAX\n",
      "使用最大最小值标准化连续特征 DEBT_DAY_LOAN_BAL\n",
      "使用最大最小值标准化连续特征 DEBT_MAVER_LOAN_BAL\n",
      "使用最大最小值标准化连续特征 DEBT_SAVER_LOAN_BAL\n",
      "使用最大最小值标准化连续特征 DEBT_YAVER_LOAN_BAL\n",
      "使用最大最小值标准化连续特征 ast_day_aum_bal\n",
      "使用最大最小值标准化连续特征 ast_day_otr_bal\n",
      "使用最大最小值标准化连续特征 ast_maver_aum_bal\n",
      "使用最大最小值标准化连续特征 ast_maver_otr_bal\n",
      "使用最大最小值标准化连续特征 ast_saver_aum_bal\n",
      "使用最大最小值标准化连续特征 ast_saver_otr_bal\n",
      "使用最大最小值标准化连续特征 ast_yaver_aum_bal\n",
      "使用最大最小值标准化连续特征 ast_yaver_otr_bal\n",
      "使用最大最小值标准化连续特征 ast_day_diff_maver_aum\n",
      "使用最大最小值标准化连续特征 ast_day_diff_maver_otr\n",
      "使用最大最小值标准化连续特征 ast_day_diff_maver_loan\n",
      "使用最大最小值标准化连续特征 ast_day_diff_saver_aum\n",
      "使用最大最小值标准化连续特征 ast_day_diff_saver_otr\n",
      "使用最大最小值标准化连续特征 ast_day_diff_saver_loan\n",
      "使用最大最小值标准化连续特征 ast_day_diff_yaver_aum\n",
      "使用最大最小值标准化连续特征 ast_day_diff_yaver_otr\n",
      "使用最大最小值标准化连续特征 ast_day_diff_yaver_loan\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_saver_aum\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_saver_otr\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_saver_loan\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_yaver_aum\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_yaver_otr\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_yaver_loan\n",
      "使用最大最小值标准化连续特征 ast_saver_diff_yaver_aum\n",
      "使用最大最小值标准化连续特征 ast_saver_diff_yaver_otr\n",
      "使用最大最小值标准化连续特征 ast_saver_diff_yaver_loan\n",
      "使用最大最小值标准化连续特征 ast_day_diff_max_fa\n",
      "使用最大最小值标准化连续特征 ast_day_diff_maver_fa\n",
      "使用最大最小值标准化连续特征 ast_day_diff_saver_fa\n",
      "使用最大最小值标准化连续特征 ast_day_diff_yaver_fa\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_max_fa\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_saver_fa\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_yaver_fa\n",
      "使用最大最小值标准化连续特征 ast_saver_diff_max_fa\n",
      "使用最大最小值标准化连续特征 ast_saver_diff_yaver_fa\n",
      "使用最大最小值标准化连续特征 ast_yaver_diff_max_fa\n",
      "使用最大最小值标准化连续特征 ast_day_diff_max_dp\n",
      "使用最大最小值标准化连续特征 ast_day_diff_maver_dp\n",
      "使用最大最小值标准化连续特征 ast_day_diff_saver_dp\n",
      "使用最大最小值标准化连续特征 ast_day_diff_yaver_dp\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_max_dp\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_saver_dp\n",
      "使用最大最小值标准化连续特征 ast_maver_diff_yaver_dp\n",
      "使用最大最小值标准化连续特征 ast_saver_diff_max_dp\n",
      "使用最大最小值标准化连续特征 ast_saver_diff_yaver_dp\n",
      "使用最大最小值标准化连续特征 ast_yaver_diff_max_dp\n",
      "使用最大最小值标准化连续特征 aget_day_diff_max\n",
      "使用最大最小值标准化连续特征 aget_day_diff_min\n",
      "使用最大最小值标准化连续特征 aps_sum_amt\n",
      "使用最大最小值标准化连续特征 aps_sum_abs_amt\n",
      "使用最大最小值标准化连续特征 aps_sum_avg_amt\n",
      "使用最大最小值标准化连续特征 aps_max_amt\n",
      "使用最大最小值标准化连续特征 aps_min_amt\n",
      "使用最大最小值标准化连续特征 aps_last_diff\n",
      "使用最大最小值标准化连续特征 aps_code_cnt\n",
      "使用最大最小值标准化连续特征 aps_chl_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs1_maxamt\n",
      "使用最大最小值标准化连续特征 aps_abs2_maxamt\n",
      "使用最大最小值标准化连续特征 aps_abs3_maxamt\n",
      "使用最大最小值标准化连续特征 aps_abs1_minamt\n",
      "使用最大最小值标准化连续特征 aps_abs2_minamt\n",
      "使用最大最小值标准化连续特征 aps_abs3_minamt\n",
      "使用最大最小值标准化连续特征 aps_abs1_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs2_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs3_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs4_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs5_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs6_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs7_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs8_cnt\n",
      "使用最大最小值标准化连续特征 aps_abs9_cnt\n",
      "使用最大最小值标准化连续特征 aps_code1_ind\n",
      "使用最大最小值标准化连续特征 aps_code2_ind\n",
      "使用最大最小值标准化连续特征 aps_code3_ind\n",
      "使用最大最小值标准化连续特征 aps_code4_ind\n",
      "使用最大最小值标准化连续特征 aps_code5_ind\n",
      "使用最大最小值标准化连续特征 aps_code6_ind\n",
      "使用最大最小值标准化连续特征 aps_code7_ind\n",
      "使用最大最小值标准化连续特征 aps_code8_ind\n",
      "使用最大最小值标准化连续特征 aps_code9_ind\n",
      "使用最大最小值标准化连续特征 aps_code10_ind\n",
      "使用最大最小值标准化连续特征 aps_code11_ind\n",
      "使用最大最小值标准化连续特征 aps_code12_ind\n",
      "使用最大最小值标准化连续特征 aps_code13_ind\n",
      "使用最大最小值标准化连续特征 aps_code14_ind\n",
      "使用最大最小值标准化连续特征 aps_code15_ind\n",
      "使用最大最小值标准化连续特征 aps_chl1_ind\n",
      "使用最大最小值标准化连续特征 aps_chl2_ind\n",
      "使用最大最小值标准化连续特征 aps_chl3_ind\n",
      "使用最大最小值标准化连续特征 aps_chl4_ind\n",
      "使用最大最小值标准化连续特征 aps_chl5_ind\n",
      "使用最大最小值标准化连续特征 aps_chl6_ind\n",
      "使用最大最小值标准化连续特征 aps_chl7_ind\n",
      "使用最大最小值标准化连续特征 aps_chl8_ind\n",
      "使用最大最小值标准化连续特征 aps_chl9_ind\n",
      "使用最大最小值标准化连续特征 aps_chl10_ind\n",
      "使用最大最小值标准化连续特征 aps_chl11_ind\n",
      "使用最大最小值标准化连续特征 aps_chl12_ind\n",
      "使用最大最小值标准化连续特征 aps_chl13_ind\n",
      "使用最大最小值标准化连续特征 aps_chl14_ind\n",
      "使用最大最小值标准化连续特征 aps_chl15_ind\n",
      "使用最大最小值标准化连续特征 aps_chl16_ind\n",
      "使用最大最小值标准化连续特征 aps_chl17_ind\n",
      "标准化完成！\n"
     ]
    }
   ],
   "source": [
    "data2 = pd.concat([df_train, df_test], axis=0)\n",
    "std_feas = get_continuous_standard(data2[features] , 0)\n",
    "\n",
    "for f in features :\n",
    "    data2[f] = std_feas[f]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:35.806805Z",
     "iopub.status.busy": "2023-11-13T13:30:35.806607Z",
     "iopub.status.idle": "2023-11-13T13:30:36.209973Z",
     "shell.execute_reply": "2023-11-13T13:30:36.209352Z",
     "shell.execute_reply.started": "2023-11-13T13:30:35.806780Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "data3 = data2\n",
    "df_train = data3[data3.TD_IND.notnull()].reset_index(drop=True)\n",
    "df_test = data3[data3.TD_IND.isnull()].reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:36.211156Z",
     "iopub.status.busy": "2023-11-13T13:30:36.210951Z",
     "iopub.status.idle": "2023-11-13T13:30:36.213960Z",
     "shell.execute_reply": "2023-11-13T13:30:36.213351Z",
     "shell.execute_reply.started": "2023-11-13T13:30:36.211131Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "seed=2023"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:30:36.215020Z",
     "iopub.status.busy": "2023-11-13T13:30:36.214845Z",
     "iopub.status.idle": "2023-11-13T13:31:08.484268Z",
     "shell.execute_reply": "2023-11-13T13:31:08.483599Z",
     "shell.execute_reply.started": "2023-11-13T13:30:36.214998Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "res1 = pd.DataFrame(df_test['CUST_NO'])\n",
    "res2 = pd.DataFrame(df_test['CUST_NO'])\n",
    "\n",
    "def kfold_stats_feature(train, test, feats, k,label='label'):\n",
    "    folds = StratifiedKFold(n_splits=k, shuffle=True, random_state=seed)  # 这里最好和后面模型的K折交叉验证保持一致\n",
    "\n",
    "    train['fold'] = None\n",
    "    for fold_, (trn_idx, val_idx) in enumerate(folds.split(train, train[label])):\n",
    "        train.loc[val_idx, 'fold'] = fold_\n",
    "\n",
    "    kfold_features = []\n",
    "    for feat in feats:\n",
    "        nums_columns = [label]\n",
    "        for f in nums_columns:\n",
    "            colname = feat + '_' + f + '_kfold_mean'\n",
    "            kfold_features.append(colname)\n",
    "            train[colname] = None\n",
    "            for fold_, (trn_idx, val_idx) in enumerate(folds.split(train, train[label])):\n",
    "                tmp_trn = train.iloc[trn_idx]\n",
    "                order_label = tmp_trn.groupby([feat])[f].mean()\n",
    "                tmp = train.loc[train.fold == fold_, [feat]]\n",
    "                train.loc[train.fold == fold_, colname] = tmp[feat].map(order_label)\n",
    "                # fillna\n",
    "                global_mean = train[f].mean()\n",
    "                train.loc[train.fold == fold_, colname] = train.loc[train.fold == fold_, colname].fillna(global_mean)\n",
    "            train[colname] = train[colname].astype(float)\n",
    "\n",
    "        for f in nums_columns:\n",
    "            colname = feat + '_' + f + '_kfold_mean'\n",
    "            test[colname] = None\n",
    "            order_label = train.groupby([feat])[f].mean()\n",
    "            test[colname] = test[feat].map(order_label)\n",
    "            # fillna\n",
    "            global_mean = train[f].mean()\n",
    "            test[colname] = test[colname].fillna(global_mean)\n",
    "            test[colname] = test[colname].astype(float)\n",
    "    del train['fold']\n",
    "    return train, test\n",
    "\n",
    "target_encoder_columns = [\n",
    "'prov_cd'\n",
    ",'unit_typ_cd'\n",
    ",'MB_REG_PROV'\n",
    ",'MS_IND'\n",
    ",'NTRL_CUST_SEX_CD'\n",
    ",'NTRL_RANK_CD'\n",
    "]\n",
    "\n",
    "for label in labels:\n",
    "    df_train, df_test = kfold_stats_feature(df_train, df_test, target_encoder_columns, 7, label=label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:31:08.485460Z",
     "iopub.status.busy": "2023-11-13T13:31:08.485257Z",
     "iopub.status.idle": "2023-11-13T13:31:08.489369Z",
     "shell.execute_reply": "2023-11-13T13:31:08.488670Z",
     "shell.execute_reply.started": "2023-11-13T13:31:08.485434Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "labels = ['TD_IND','FNCG_IND','FUND_IND','INSUR_IND','IL_IND']\n",
    "features = [x for x in df_train.columns if x not in labels+['label', 'DATA_DAT', 'CUST_NO']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:31:08.490177Z",
     "iopub.status.busy": "2023-11-13T13:31:08.490003Z",
     "iopub.status.idle": "2023-11-13T13:31:08.493708Z",
     "shell.execute_reply": "2023-11-13T13:31:08.493218Z",
     "shell.execute_reply.started": "2023-11-13T13:31:08.490155Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "173"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:31:08.494678Z",
     "iopub.status.busy": "2023-11-13T13:31:08.494506Z",
     "iopub.status.idle": "2023-11-13T13:42:05.295359Z",
     "shell.execute_reply": "2023-11-13T13:42:05.294745Z",
     "shell.execute_reply.started": "2023-11-13T13:31:08.494656Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=======================TD_IND=======================\n",
      "************************************ 2023 - 1 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.937532\tvalid_1's auc: 0.808088\n",
      "Early stopping, best iteration is:\n",
      "[454]\ttraining's auc: 0.930889\tvalid_1's auc: 0.808572\n",
      "[0.8085715049594954]\n",
      "************************************ 2023 - 2 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.937382\tvalid_1's auc: 0.816129\n",
      "Early stopping, best iteration is:\n",
      "[324]\ttraining's auc: 0.907599\tvalid_1's auc: 0.81723\n",
      "[0.8085715049594954, 0.8172303593957386]\n",
      "************************************ 2023 - 3 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.936722\tvalid_1's auc: 0.808446\n",
      "Early stopping, best iteration is:\n",
      "[194]\ttraining's auc: 0.879578\tvalid_1's auc: 0.811972\n",
      "[0.8085715049594954, 0.8172303593957386, 0.8119719185789204]\n",
      "************************************ 2023 - 4 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.93598\tvalid_1's auc: 0.824298\n",
      "Early stopping, best iteration is:\n",
      "[234]\ttraining's auc: 0.887043\tvalid_1's auc: 0.825299\n",
      "[0.8085715049594954, 0.8172303593957386, 0.8119719185789204, 0.8252993858970888]\n",
      "************************************ 2023 - 5 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.935388\tvalid_1's auc: 0.818528\n",
      "Early stopping, best iteration is:\n",
      "[291]\ttraining's auc: 0.899235\tvalid_1's auc: 0.819928\n",
      "[0.8085715049594954, 0.8172303593957386, 0.8119719185789204, 0.8252993858970888, 0.8199275868538604]\n",
      "************************************ 2023 - 6 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.935899\tvalid_1's auc: 0.824741\n",
      "Early stopping, best iteration is:\n",
      "[253]\ttraining's auc: 0.891484\tvalid_1's auc: 0.826855\n",
      "[0.8085715049594954, 0.8172303593957386, 0.8119719185789204, 0.8252993858970888, 0.8199275868538604, 0.8268550698363349]\n",
      "************************************ 2023 - 7 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.936844\tvalid_1's auc: 0.820915\n",
      "Early stopping, best iteration is:\n",
      "[161]\ttraining's auc: 0.869637\tvalid_1's auc: 0.82419\n",
      "[0.8085715049594954, 0.8172303593957386, 0.8119719185789204, 0.8252993858970888, 0.8199275868538604, 0.8268550698363349, 0.8241897132895295]\n",
      "lgb_scotrainre_list: [0.8085715049594954, 0.8172303593957386, 0.8119719185789204, 0.8252993858970888, 0.8199275868538604, 0.8268550698363349, 0.8241897132895295]\n",
      "lgb_score_mean: 0.8191493626872811\n",
      "lgb_score_std: 0.006439840579948462\n",
      "trn_f1_scotrainre_list: [0.6005120078020236, 0.536664180959483, 0.46393357550596787, 0.48116674322462105, 0.5089185480853188, 0.4934531450577664, 0.44632516703786185]\n",
      "trn_f1_score_mean: 0.5044247668104347\n",
      "trn_f1_score_std: 0.04776242394570684\n",
      "val_f1_scotrainre_list: [0.353757225433526, 0.3509781357882624, 0.35209713024282563, 0.34153400868306805, 0.33539780848022865, 0.36786469344608874, 0.38061585058051495]\n",
      "val_f1_score_mean: 0.3546064075220735\n",
      "val_f1_score_std: 0.014204190030046339\n",
      "=======================FNCG_IND=======================\n",
      "************************************ 2023 - 1 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.982919\tvalid_1's auc: 0.919557\n",
      "[1000]\ttraining's auc: 0.997762\tvalid_1's auc: 0.917352\n",
      "Early stopping, best iteration is:\n",
      "[563]\ttraining's auc: 0.986316\tvalid_1's auc: 0.920076\n",
      "[0.9200757044721717]\n",
      "************************************ 2023 - 2 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.982535\tvalid_1's auc: 0.928365\n",
      "Early stopping, best iteration is:\n",
      "[346]\ttraining's auc: 0.971385\tvalid_1's auc: 0.928809\n",
      "[0.9200757044721717, 0.9288089891375023]\n",
      "************************************ 2023 - 3 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.982497\tvalid_1's auc: 0.918756\n",
      "Early stopping, best iteration is:\n",
      "[339]\ttraining's auc: 0.970978\tvalid_1's auc: 0.919243\n",
      "[0.9200757044721717, 0.9288089891375023, 0.9192432312526062]\n",
      "************************************ 2023 - 4 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.983385\tvalid_1's auc: 0.911361\n",
      "Early stopping, best iteration is:\n",
      "[39]\ttraining's auc: 0.92553\tvalid_1's auc: 0.913461\n",
      "[0.9200757044721717, 0.9288089891375023, 0.9192432312526062, 0.9134606890645757]\n",
      "************************************ 2023 - 5 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.983236\tvalid_1's auc: 0.90364\n",
      "Early stopping, best iteration is:\n",
      "[411]\ttraining's auc: 0.977462\tvalid_1's auc: 0.904493\n",
      "[0.9200757044721717, 0.9288089891375023, 0.9192432312526062, 0.9134606890645757, 0.9044927956097675]\n",
      "************************************ 2023 - 6 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.982865\tvalid_1's auc: 0.914237\n",
      "[1000]\ttraining's auc: 0.997862\tvalid_1's auc: 0.91264\n",
      "Early stopping, best iteration is:\n",
      "[507]\ttraining's auc: 0.983307\tvalid_1's auc: 0.914362\n",
      "[0.9200757044721717, 0.9288089891375023, 0.9192432312526062, 0.9134606890645757, 0.9044927956097675, 0.9143620731856567]\n",
      "************************************ 2023 - 7 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.982537\tvalid_1's auc: 0.925644\n",
      "Early stopping, best iteration is:\n",
      "[241]\ttraining's auc: 0.96035\tvalid_1's auc: 0.926801\n",
      "[0.9200757044721717, 0.9288089891375023, 0.9192432312526062, 0.9134606890645757, 0.9044927956097675, 0.9143620731856567, 0.9268006886853527]\n",
      "lgb_scotrainre_list: [0.9200757044721717, 0.9288089891375023, 0.9192432312526062, 0.9134606890645757, 0.9044927956097675, 0.9143620731856567, 0.9268006886853527]\n",
      "lgb_score_mean: 0.9181777387725188\n",
      "lgb_score_std: 0.007710881260827739\n",
      "trn_f1_scotrainre_list: [0.8094190140845071, 0.7005912162162161, 0.7042437431991294, 0.48460812086874405, 0.7426963860636225, 0.792882267742603, 0.6366666666666667]\n",
      "trn_f1_score_mean: 0.6958724878344984\n",
      "trn_f1_score_std: 0.1019288931350483\n",
      "val_f1_scotrainre_list: [0.47529812606473587, 0.5048923679060665, 0.4897025171624714, 0.45525291828793774, 0.44198895027624313, 0.4549019607843138, 0.46495327102803735]\n",
      "val_f1_score_mean: 0.4695700159299722\n",
      "val_f1_score_std: 0.020311706840595776\n",
      "=======================FUND_IND=======================\n",
      "************************************ 2023 - 1 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.982033\tvalid_1's auc: 0.89198\n",
      "Early stopping, best iteration is:\n",
      "[108]\ttraining's auc: 0.934935\tvalid_1's auc: 0.894484\n",
      "[0.8944840743198407]\n",
      "************************************ 2023 - 2 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.982124\tvalid_1's auc: 0.902483\n",
      "Early stopping, best iteration is:\n",
      "[105]\ttraining's auc: 0.931471\tvalid_1's auc: 0.904742\n",
      "[0.8944840743198407, 0.90474213581747]\n",
      "************************************ 2023 - 3 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.98154\tvalid_1's auc: 0.897818\n",
      "Early stopping, best iteration is:\n",
      "[217]\ttraining's auc: 0.953614\tvalid_1's auc: 0.900185\n",
      "[0.8944840743198407, 0.90474213581747, 0.9001852014109508]\n",
      "************************************ 2023 - 4 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.982146\tvalid_1's auc: 0.89801\n",
      "Early stopping, best iteration is:\n",
      "[274]\ttraining's auc: 0.962529\tvalid_1's auc: 0.899029\n",
      "[0.8944840743198407, 0.90474213581747, 0.9001852014109508, 0.8990288608262187]\n",
      "************************************ 2023 - 5 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.981965\tvalid_1's auc: 0.904794\n",
      "Early stopping, best iteration is:\n",
      "[184]\ttraining's auc: 0.948265\tvalid_1's auc: 0.907547\n",
      "[0.8944840743198407, 0.90474213581747, 0.9001852014109508, 0.8990288608262187, 0.9075467629667369]\n",
      "************************************ 2023 - 6 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.981705\tvalid_1's auc: 0.910196\n",
      "Early stopping, best iteration is:\n",
      "[99]\ttraining's auc: 0.930727\tvalid_1's auc: 0.912946\n",
      "[0.8944840743198407, 0.90474213581747, 0.9001852014109508, 0.8990288608262187, 0.9075467629667369, 0.9129458811525666]\n",
      "************************************ 2023 - 7 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.981795\tvalid_1's auc: 0.906009\n",
      "Early stopping, best iteration is:\n",
      "[378]\ttraining's auc: 0.972619\tvalid_1's auc: 0.907651\n",
      "[0.8944840743198407, 0.90474213581747, 0.9001852014109508, 0.8990288608262187, 0.9075467629667369, 0.9129458811525666, 0.9076511490234178]\n",
      "lgb_scotrainre_list: [0.8944840743198407, 0.90474213581747, 0.9001852014109508, 0.8990288608262187, 0.9075467629667369, 0.9129458811525666, 0.9076511490234178]\n",
      "lgb_score_mean: 0.9037977236453145\n",
      "lgb_score_std: 0.005806662630678355\n",
      "trn_f1_scotrainre_list: [0.5071225071225071, 0.4933652381988254, 0.6036815258372145, 0.6498277841561424, 0.5783185840707965, 0.489922480620155, 0.7215628090999011]\n",
      "trn_f1_score_mean: 0.5776858470150774\n",
      "trn_f1_score_std: 0.08139214903849823\n",
      "val_f1_scotrainre_list: [0.38175270108043213, 0.3928170594837262, 0.3590859630032644, 0.4018058690744921, 0.40537513997760355, 0.4198757763975155, 0.4122137404580153]\n",
      "val_f1_score_mean: 0.39613232135357845\n",
      "val_f1_score_std: 0.019011949795474783\n",
      "=======================INSUR_IND=======================\n",
      "************************************ 2023 - 1 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.977186\tvalid_1's auc: 0.850677\n",
      "Early stopping, best iteration is:\n",
      "[193]\ttraining's auc: 0.924514\tvalid_1's auc: 0.854986\n",
      "[0.8549862860588413]\n",
      "************************************ 2023 - 2 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.978303\tvalid_1's auc: 0.842069\n",
      "Early stopping, best iteration is:\n",
      "[348]\ttraining's auc: 0.958435\tvalid_1's auc: 0.845851\n",
      "[0.8549862860588413, 0.8458509282573505]\n",
      "************************************ 2023 - 3 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.976404\tvalid_1's auc: 0.848645\n",
      "Early stopping, best iteration is:\n",
      "[172]\ttraining's auc: 0.917264\tvalid_1's auc: 0.855358\n",
      "[0.8549862860588413, 0.8458509282573505, 0.8553576719491981]\n",
      "************************************ 2023 - 4 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.977669\tvalid_1's auc: 0.840014\n",
      "Early stopping, best iteration is:\n",
      "[286]\ttraining's auc: 0.947766\tvalid_1's auc: 0.842336\n",
      "[0.8549862860588413, 0.8458509282573505, 0.8553576719491981, 0.8423358950235557]\n",
      "************************************ 2023 - 5 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.977995\tvalid_1's auc: 0.84582\n",
      "Early stopping, best iteration is:\n",
      "[198]\ttraining's auc: 0.925511\tvalid_1's auc: 0.849771\n",
      "[0.8549862860588413, 0.8458509282573505, 0.8553576719491981, 0.8423358950235557, 0.8497712465939291]\n",
      "************************************ 2023 - 6 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.977344\tvalid_1's auc: 0.853139\n",
      "Early stopping, best iteration is:\n",
      "[278]\ttraining's auc: 0.94328\tvalid_1's auc: 0.857129\n",
      "[0.8549862860588413, 0.8458509282573505, 0.8553576719491981, 0.8423358950235557, 0.8497712465939291, 0.8571286627753497]\n",
      "************************************ 2023 - 7 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.976937\tvalid_1's auc: 0.846085\n",
      "Early stopping, best iteration is:\n",
      "[277]\ttraining's auc: 0.943746\tvalid_1's auc: 0.851254\n",
      "[0.8549862860588413, 0.8458509282573505, 0.8553576719491981, 0.8423358950235557, 0.8497712465939291, 0.8571286627753497, 0.8512542244878694]\n",
      "lgb_scotrainre_list: [0.8549862860588413, 0.8458509282573505, 0.8553576719491981, 0.8423358950235557, 0.8497712465939291, 0.8571286627753497, 0.8512542244878694]\n",
      "lgb_score_mean: 0.8509549878780133\n",
      "lgb_score_std: 0.005008282083748587\n",
      "trn_f1_scotrainre_list: [0.49738100660441814, 0.608738828202582, 0.4764162123726001, 0.5635, 0.49437125748502997, 0.5532435740514076, 0.5528306211322485]\n",
      "trn_f1_score_mean: 0.5352116428354694\n",
      "trn_f1_score_std: 0.04374937360402038\n",
      "val_f1_scotrainre_list: [0.33378196500672946, 0.3372365339578454, 0.3236514522821577, 0.3485254691689008, 0.31891891891891894, 0.3464566929133858, 0.34357541899441335]\n",
      "val_f1_score_mean: 0.3360209216060502\n",
      "val_f1_score_std: 0.01051741664797852\n",
      "=======================IL_IND=======================\n",
      "************************************ 2023 - 1 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.975303\tvalid_1's auc: 0.916211\n",
      "Early stopping, best iteration is:\n",
      "[227]\ttraining's auc: 0.948315\tvalid_1's auc: 0.918154\n",
      "[0.9181542053096113]\n",
      "************************************ 2023 - 2 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.976458\tvalid_1's auc: 0.897869\n",
      "Early stopping, best iteration is:\n",
      "[319]\ttraining's auc: 0.960773\tvalid_1's auc: 0.899079\n",
      "[0.9181542053096113, 0.8990794196255392]\n",
      "************************************ 2023 - 3 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.97519\tvalid_1's auc: 0.913855\n",
      "Early stopping, best iteration is:\n",
      "[197]\ttraining's auc: 0.94396\tvalid_1's auc: 0.916036\n",
      "[0.9181542053096113, 0.8990794196255392, 0.9160355537438353]\n",
      "************************************ 2023 - 4 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.976158\tvalid_1's auc: 0.894765\n",
      "Early stopping, best iteration is:\n",
      "[417]\ttraining's auc: 0.969974\tvalid_1's auc: 0.89574\n",
      "[0.9181542053096113, 0.8990794196255392, 0.9160355537438353, 0.8957400691267839]\n",
      "************************************ 2023 - 5 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.97537\tvalid_1's auc: 0.914577\n",
      "Early stopping, best iteration is:\n",
      "[276]\ttraining's auc: 0.954519\tvalid_1's auc: 0.916007\n",
      "[0.9181542053096113, 0.8990794196255392, 0.9160355537438353, 0.8957400691267839, 0.9160065960217744]\n",
      "************************************ 2023 - 6 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.975367\tvalid_1's auc: 0.903395\n",
      "Early stopping, best iteration is:\n",
      "[340]\ttraining's auc: 0.961887\tvalid_1's auc: 0.904834\n",
      "[0.9181542053096113, 0.8990794196255392, 0.9160355537438353, 0.8957400691267839, 0.9160065960217744, 0.9048338835052422]\n",
      "************************************ 2023 - 7 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "[LightGBM] [Warning] Unknown parameter: silent\n",
      "Training until validation scores don't improve for 500 rounds\n",
      "[500]\ttraining's auc: 0.97534\tvalid_1's auc: 0.912661\n",
      "[1000]\ttraining's auc: 0.995156\tvalid_1's auc: 0.911159\n",
      "Early stopping, best iteration is:\n",
      "[679]\ttraining's auc: 0.98536\tvalid_1's auc: 0.913236\n",
      "[0.9181542053096113, 0.8990794196255392, 0.9160355537438353, 0.8957400691267839, 0.9160065960217744, 0.9048338835052422, 0.9132358842183851]\n",
      "lgb_scotrainre_list: [0.9181542053096113, 0.8990794196255392, 0.9160355537438353, 0.8957400691267839, 0.9160065960217744, 0.9048338835052422, 0.9132358842183851]\n",
      "lgb_score_mean: 0.9090122302215959\n",
      "lgb_score_std: 0.008383084631609366\n",
      "trn_f1_scotrainre_list: [0.6221719457013574, 0.6729902037008242, 0.6100056850483229, 0.707338638373121, 0.6456325769350326, 0.6732363522602847, 0.7918227215980025]\n",
      "trn_f1_score_mean: 0.674742589088135\n",
      "trn_f1_score_std: 0.05679226753171592\n",
      "val_f1_scotrainre_list: [0.5473145780051151, 0.5214953271028038, 0.5377821393523061, 0.49330783938814526, 0.5454545454545455, 0.5296803652968037, 0.5461613216715256]\n",
      "val_f1_score_mean: 0.5315994451816064\n",
      "val_f1_score_std: 0.017973100143373992\n"
     ]
    }
   ],
   "source": [
    "X_train, Y_train = df_train[features], df_train[labels]\n",
    "X_test = df_test[features]\n",
    "for label in [ 'TD_IND' ]:\n",
    "    print(\"======================={}=======================\".format(label))\n",
    "    lgb_train, lgb_test, lgb_testtp, models = lgb_model(X_train, Y_train[label], X_test, seed, threshold= 6/7 -0.01)\n",
    "    res1[label] =  pd.DataFrame( lgb_test )\n",
    "    res2[label] =  pd.DataFrame( lgb_testtp )\n",
    "\n",
    "for label in [ 'FNCG_IND', 'FUND_IND', 'INSUR_IND' ]:\n",
    "    print(\"======================={}=======================\".format(label))\n",
    "    lgb_train, lgb_test, lgb_testtp, models = lgb_model(X_train, Y_train[label], X_test, seed, threshold= 5/7 -0.01)\n",
    "    res1[label] =  pd.DataFrame( lgb_test )\n",
    "    res2[label] =  pd.DataFrame( lgb_testtp )\n",
    "\n",
    "for label in ['IL_IND']:\n",
    "    print(\"======================={}=======================\".format(label))\n",
    "    lgb_train, lgb_test, lgb_testtp, models = lgb_model(X_train, Y_train[label], X_test, seed, threshold= 3/7 -0.01)\n",
    "    res1[label] =  pd.DataFrame( lgb_test )\n",
    "    res2[label] =  pd.DataFrame( lgb_testtp )\n",
    "res2.to_csv('lgb_{}_{}.py'.format(str(X_train.shape[1]), str(seed)), header=False, index=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:42:05.296588Z",
     "iopub.status.busy": "2023-11-13T13:42:05.296377Z",
     "iopub.status.idle": "2023-11-13T13:42:05.529945Z",
     "shell.execute_reply": "2023-11-13T13:42:05.529368Z",
     "shell.execute_reply.started": "2023-11-13T13:42:05.296562Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Magic init complete.\n",
      "Predict init complete.\n",
      "Matplotlib env init complete.\n",
      "Gbase数据库信息配置为空，相关魔法命令不可使用（%sql, %df2db等），如有需求，请联系管理员配置或自行配置\n"
     ]
    }
   ],
   "source": [
    "init_woody"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:42:05.531106Z",
     "iopub.status.busy": "2023-11-13T13:42:05.530888Z",
     "iopub.status.idle": "2023-11-13T13:42:05.932224Z",
     "shell.execute_reply": "2023-11-13T13:42:05.931746Z",
     "shell.execute_reply.started": "2023-11-13T13:42:05.531071Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'请稍后使用命令: %query_predict problem_id 查看评分结果, problem_id为阶段序号，取值为：1,2, 比如查询第一阶段的评分结果: %query_predict 1'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%predict 6 lgb_173_2023.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-13T13:42:13.261834Z",
     "iopub.status.busy": "2023-11-13T13:42:13.261547Z",
     "iopub.status.idle": "2023-11-13T13:42:13.533047Z",
     "shell.execute_reply": "2023-11-13T13:42:13.532395Z",
     "shell.execute_reply.started": "2023-11-13T13:42:13.261805Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最近三次评分提交的结果供参考(大模型程序评分需要一些时间，请耐心等待，只有评分完成的结果会显示在下面的列表中):\n",
      "\n",
      "提交时间：2023-11-13 21:42:05 \t 评分结果：0.4179     \t 评分成功\n",
      "提交时间：2023-11-13 21:13:45 \t 评分结果：0.4179     \t 评分成功\n",
      "提交时间：2023-11-13 20:59:44 \t 评分结果：0.4113     \t 评分成功\n"
     ]
    }
   ],
   "source": [
    "%query_predict 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc-showcode": false
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
